package org.testis.gui.mvc.utilities;

import org.testis.utils.tree.MyDefaultMutableTreeNode;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Locale;
import javax.swing.JCheckBox;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class MyTreeRenderer extends DefaultTreeCellRenderer {

    private static final long serialVersionUID = 32423L;
    protected JCheckBox m_check;
    protected TreeLabel m_label;

    public MyTreeRenderer() {
        setLayout(null);
        m_check = new JCheckBox();
        m_check.setBackground(UIManager.getColor("Tree.textBackground"));
        add(m_label = new TreeLabel());
        m_label.setForeground(UIManager.getColor("Tree.textForeground"));
    }

    @Override
    public Component getTreeCellRendererComponent(
            JTree tree,
            Object value,
            boolean sel,
            boolean expanded,
            boolean leaf,
            int row,
            boolean hasFocus) {
        String stringValue;
        try {
            stringValue = tree.convertValueToText(value,
                    sel,
                    expanded,
                    leaf,
                    row,
                    hasFocus);
        } catch (NullPointerException ex) {
            return this;
        }
        String name;
        MyDefaultMutableTreeNode node = (MyDefaultMutableTreeNode) value;
        Dimension d_check = m_check.getPreferredSize();
        Dimension d_label = m_label.getPreferredSize();
        int y_check = 0;
        int y_label = 0;
        int checkHeight = 0;
        int checkWidth = 0;
        if (node.hasCheckBox()) {
            add(m_check);
            checkHeight = d_check.height;
            checkWidth = d_check.width;
            m_check.setLocation(0,
                    y_check);
            m_check.setBounds(0,
                    y_check,
                    d_check.width,
                    d_check.height);
            m_check.setBackground(Color.WHITE);
            m_check.setSelected(node.isSelected());
            m_check.setEnabled(node.isCheckBoxSelectable());
            m_check.setVisible(true);
        } else {
            m_check.setVisible(false);
        }
        if (checkHeight < d_label.height) {
            y_check = (d_label.height - checkHeight) / 2;
        } else {
            y_label = (checkHeight - d_label.height) / 2;
        }
        m_label.setLocation(checkWidth,
                y_label);
        m_label.setBounds(checkWidth,
                y_label,
                d_label.width,
                d_label.height);
        Object object = node.getUserObject();
        if (object instanceof TestDesignTreeNode) {
            TestDesignTreeNode treeNode = (TestDesignTreeNode) object;
            if (treeNode.getObject() == null) {
                return null;
            }
            name = treeNode.getTypeName();
            String imageName = name.toLowerCase(Locale.ENGLISH);
            if (treeNode.isClone()) {
                imageName = imageName + "clone";
            }
            imageName = imageName + ".png";
            ImageIcon imageIcon = createImageIcon("images/testdesign/" +
                    imageName);
            m_label.setIcon(imageIcon);
            setToolTipText(name);
            boolean isValid=treeNode.isValid();
            if(!isValid){
              m_label.setForeground(Color.RED);
            }
            else
              m_label.setForeground(Color.BLACK);
        } else if (object instanceof DynamicMapDesignTreeNode) {
            DynamicMapDesignTreeNode treeNode =
                    (DynamicMapDesignTreeNode) object;
            name = treeNode.getObject().getGroupName();
            String typeName = treeNode.getObject().getReference().getType().
                    getName();
            String imageName = "";

            if (name.equals("Behaviour")) {
                imageName = name.toLowerCase(Locale.ENGLISH);

            } else {
                imageName = typeName.toLowerCase(Locale.ENGLISH);
            }

            if (treeNode.isClone()) {
                imageName = imageName + "clone";
            }
            imageName = imageName + ".png";
            ImageIcon imageIcon = createImageIcon("images/dynamicmap/" +
                    imageName);
            m_label.setIcon(imageIcon);
            setToolTipText(name);
            m_check.setSelected(((MyDefaultMutableTreeNode) value).isSelected());
            boolean isValid=treeNode.isValid();
             if(!isValid){
              m_label.setForeground(Color.RED);
            }
            else
              m_label.setForeground(Color.BLACK);
        } else {
            //Dummy TreeNode
            //label.setIcon(null);
            setToolTipText(null); //no tool tip
            if (expanded) {
                m_label.setIcon(UIManager.getIcon("Tree.openIcon"));
            } else {
                m_label.setIcon(UIManager.getIcon("Tree.closedIcon"));
            }
        }
        setEnabled(tree.isEnabled());
        m_label.setFont(tree.getFont());
        m_label.setText(stringValue);
        m_label.setSize(2000,
                16);
        m_label.setSelected(sel);
        m_label.setFocus(hasFocus);
        Color selectedColor;
        if (node.isHighlighted()) {
            selectedColor = node.getHighlightColor();
        } else {
            selectedColor = node.getColor();
        }
        m_label.setBackground(selectedColor);
        if (!selectedColor.equals(Color.WHITE)) {
            m_label.setOpaque(true);
        } else {
            m_label.setOpaque(false);
        }
        return this;
    }

    public static ImageIcon createImageIcon(String path) {
        return ImageFactory.instance().createImageIcon(path);
    }

    @Override
    public void doLayout() {
//        Dimension d_check = m_check.getPreferredSize();
//        Dimension d_label = m_label.getPreferredSize();
//        int y_check = 0;
//        int y_label = 0;
//        if (d_check.height < d_label.height) {
//            y_check = (d_label.height - d_check.height) / 2;
//        }
//        else {
//            y_label = (d_check.height - d_label.height) / 2;
//        }
//        m_check.setLocation(0,
//                          y_check);
//        m_check.setBounds(0,
//                        y_check,
//                        d_check.width,
//                        d_check.height);
//        m_label.setLocation(d_check.width,
//                          y_label);
//        m_label.setBounds(d_check.width,
//                        y_label,
//                        d_label.width,
//                        d_label.height);
    }

    @Override
    public void setBackground(Color color) {
        if (color instanceof ColorUIResource) {
            color = null;
        }
        super.setBackground(color);
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension d_check = m_check.getPreferredSize();
        Dimension d_label = m_label.getPreferredSize();
        return new Dimension(d_check.width + d_label.width,
                (d_check.height < d_label.height ? d_label.height
                : d_check.height));
    }
}